package valve_test.forms;

import valve_test.DAO.Factory;
import valve_test.drawning.graph_pane;
import valve_test.logic.valves;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * Created with IntelliJ IDEA.
 * User: Shinigami
 * Date: 13.03.14
 * Time: 16:34
 * To change this template use File | Settings | File Templates.
 */
public class view_valves extends control {
    private JPanel view_all_panel;
    private JTable table1;
    private JButton bt_add;
    private JButton bt_exit;
    private JButton bt_edit;
    private JButton bt_ref;
    private JButton bt_del;
    private JFrame frame;

    public view_valves(){
        frame = new JFrame("Список вентилей");
        frame.getContentPane().add(view_all_panel);
        frame.setSize(520,600);
        frame.setVisible(true);
        getModel();
        bt_add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                valves v = new valves();
                v.setV_id(-1L);
                new add_valve(v, frame.getLocationOnScreen());
            }
        });
        bt_exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
            }
        });
        bt_edit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = table1.getSelectedRow(); // запоминае  выделленную строку
                if (row > -1)
                {
                    try{
                        valves v = new valves(Factory.getInstance().getValvesDAO().getValveByName(table1.getValueAt(table1.getSelectedRow(),0).toString()));
                        new add_valve(v, frame.getLocationOnScreen());
                    }
                    catch (SQLException ex){
                        System.out.println(ex.toString());
                        System.exit(-1);
                    }
                }
                else
                    JOptionPane.showMessageDialog(null, "Строка для редактирования не выделена!");
            }
        });
        bt_ref.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                getModel();
                graph_pane.ies = null;
                graph_pane.vs = null;
            }
        });
        bt_del.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = table1.getSelectedRow(); // запоминае  выделленную строку
                if (row > -1)
                {
                    try{
                        Factory.getInstance().getValvesDAO().deleteValve(Factory.getInstance().getValvesDAO().getValveByName(table1.getValueAt(table1.getSelectedRow(),0).toString()));
                        control.ViewValvesForm();
                        graph_pane.ies = null;
                        graph_pane.vs = null;
                    }
                    catch (SQLException ex){
                        System.out.println(ex.toString());
                        System.exit(-1);
                    }
                }
                else
                    JOptionPane.showMessageDialog(null, "Строка для удаления не выделена!");
            }
        });
    }

    public void getModel() {
        DefaultTableModel model = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        try {
            int columnNumber = 3;
            List<valves> valvs = new ArrayList<valves>();
            valvs = Factory.getInstance().getValvesDAO().getAllValves();
            model.setColumnCount(columnNumber);
            Vector<String> row = new Vector<String>();
            for (int i = 0; i < valvs.size(); i++)
            {
                row.add(valvs.get(i).getv_name());
                row.add(valvs.get(i).getinput().toString());
                row.add(valvs.get(i).getoutput().toString());
                model.addRow(row);
                row = new Vector<String>();
            }
            table1.setModel(model);
            TableColumnModel TableModel1;
            TableModel1 = table1.getTableHeader().getColumnModel();
            TableModel1.getColumn(0).setHeaderValue("Вентиль");
            TableModel1.getColumn(1).setHeaderValue("Число входов");
            TableModel1.getColumn(2).setHeaderValue("Число выходов");
        }
        catch (SQLException ex) {
            System.out.println(ex.toString());
            System.exit(-1);
        }
    }

    public void Show(){
        frame.setVisible(true);
    }
}
